//
//  CreatAddressViewController.m
//  1243
//
//  Created by ym on 16/5/3.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "GPDateView.h"
#import "CreatAddressViewController.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"

@interface CreatAddressViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *AddressTextField;
@property (weak, nonatomic) IBOutlet UITextField *AddressNumberTextField;
@property (weak, nonatomic) IBOutlet UILabel *shengFenLabel;
@property (weak, nonatomic) IBOutlet UILabel *CityLabel;
@property (weak, nonatomic) IBOutlet UILabel *quLabel;

@end

#define DeviceHeight            [[UIScreen mainScreen] bounds].size.height
#define DeviceWidth             [[UIScreen mainScreen] bounds].size.width
#define Key_DistrictSelectProvince          @"DistrictSelectProvince"
#define Key_DistrictSelectProvinceCode      @"DistrictSelectProvinceCode"
#define Key_DistrictSelectProvinceSubCode   @"DistrictSelectProvinceSubCode"
#define Key_DistrictSelectProvinceSub       @"DistrictSelectProvinceSub"
#define Key_DistrictSelectCityCode          @"DistrictSelectCityCode"
#define Key_DistrictSelectCity              @"DistrictSelectCity"

@implementation CreatAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"添加收货地址";
    
}
- (IBAction)didClickChooseAddressButton:(id)sender {
    [self pickerView];
}

- (IBAction)didCreatAddressButton:(id)sender {
    
    [self registerTextField];
    
    if (self.nameTextField.text.length == 0 || self.PhoneTextField.text.length == 0|| self.AddressTextField.text.length == 0|| self.AddressNumberTextField.text.length == 0) {
        
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alview.tag = 123306;
        [alview show];
        
        
    }else{
        
        NSDictionary *userData = [ReadLoginData readLoginData];
        
        NSString *userId = [userData objectForKey:@"Number"];
        
        NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd  HH:mm:ss"];
        NSString *currentTime = [formatter stringFromDate:[NSDate date]];

        NSDictionary *param = @{
                                @"Id":@"0",
                                @"UserId":userId,
                                @"Province":[NSString stringWithFormat:@"%@", self.shengFenLabel.text],
                                @"City":[NSString stringWithFormat:@"%@", self.CityLabel.text],
                                @"Countory":[NSString stringWithFormat:@"%@",self.quLabel.text],
                                @"Address":[NSString stringWithFormat:@"%@", self.AddressTextField.text],
                                @"ContactName":[NSString stringWithFormat:@"%@", self.nameTextField.text],
                                @"ContactPhone":[NSString stringWithFormat:@"%@", self.PhoneTextField.text],
                                @"ContactMobile":[NSString stringWithFormat:@"%@", self.PhoneTextField.text],
                                @"ContactPostCode":[NSString stringWithFormat:@"%@",self.AddressNumberTextField.text],
//                                @"AddTime":[NSString stringWithFormat:@"%@",currentTime],
//#warning 添加时间待修改
//                                @"AddTime":@"2016-05-03T16:10:06.6102784+08:00",
                                @"AddTime":@"",
                                @"IsDefault":@"0"
                                };
        
        [[NetWorkTools sharedNetWorkTools] objectWithURLDict:param AndMenthodName:@"EXPRESSADDRESS" completeBlock:^(id object) {
            NSLog(@"%@",object);
            
            NSInteger responceNumber = [[object objectForKey:@"Id"] integerValue];
            
            if (responceNumber == 0) {
                
                NSString *message = [NSString stringWithFormat:@"%@",[object objectForKey:@"Message"]];
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                alert.tag = 10010;
                [alert show];
            } else {
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"填写有误,请检查后重新输入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                alert.tag = 10011;
                
                [alert show];
                
            }
        }];
        
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10010) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self registerTextField];
}

- (void)registerTextField {
    [self.nameTextField resignFirstResponder];
    [self.PhoneTextField resignFirstResponder];
    [self.AddressTextField resignFirstResponder];
    [self.AddressNumberTextField resignFirstResponder];
}

- (void)pickerView{
    
    GPDateView * dateView = [[GPDateView alloc] initWithFrame:CGRectMake(0, DeviceHeight-250, DeviceWidth, 250) Data:nil];
    
    [dateView showPickerView];
    
    dateView.ActionDistrictViewSelectBlock = ^(NSString *desStr,NSDictionary *selectDistrictDict){
        self.shengFenLabel.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvince];
        self.CityLabel.text = [selectDistrictDict objectForKey:Key_DistrictSelectCity];
        self.quLabel.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvinceSub];
        
    };
    
    
    
}

@end
